package com.lwj.liwanjia.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.TkDialogAdapter;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/9.
 */

public class TuihuoDialog extends Dialog {
    private Context context;
    private TextView tv;
    private TextView tvTitle;
    private String title;
    private SuperRecyclerView rlSelect;
    private TkDialogAdapter adapter;
    private List<String> list;
    private int checkPosition = 0;
    private TextView sure;

    public TuihuoDialog(@NonNull Context context, TextView tv, String title, List<String> list) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.tv = tv;
        this.title = title;
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tuikuan_dialog);
        initView();
        initClick();
    }

    private void initClick() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                checkPosition = position;
                adapter.setCheckPosition(position);
                adapter.notifyDataSetChanged();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText(list.get(checkPosition));
                dismiss();
            }
        });
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialog_animation);
        WindowManager.LayoutParams params = window.getAttributes();
        setCanceledOnTouchOutside(false);
        params.width = params.MATCH_PARENT;
        params.height = params.WRAP_CONTENT;
        window.setAttributes(params);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(title);
        rlSelect = (SuperRecyclerView) findViewById(R.id.rl_select);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlSelect.setLayoutManager(manager);
        rlSelect.setRefreshEnabled(false);
        rlSelect.setLoadMoreEnabled(false);
        adapter = new TkDialogAdapter(context, list);
        adapter.setCheckPosition(checkPosition);
        rlSelect.setAdapter(adapter);
        sure = (TextView) findViewById(R.id.tv_sure);
    }

}
